"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

var _betterScroll = _interopRequireDefault(require("better-scroll"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _classnames = _interopRequireDefault(require("classnames"));

var _configContext = require("../../../../extends/configContext");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var PickerWrapper = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(PickerWrapper, _React$PureComponent);

  var _super = _createSuper(PickerWrapper);

  function PickerWrapper(props) {
    var _this;

    _classCallCheck(this, PickerWrapper);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "wrapper", void 0);

    _defineProperty(_assertThisInitialized(_this), "wheelScroll", void 0);

    _defineProperty(_assertThisInitialized(_this), "BScrollObj", void 0);

    _defineProperty(_assertThisInitialized(_this), "currIndex", void 0);

    _this.touchEnd = _this.touchEnd.bind(_assertThisInitialized(_this));
    _this.touchStart = _this.touchStart.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(PickerWrapper, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      if (this.props.data.selectedIndex) {
        this.BScrollObj.wheelTo(this.props.data.selectedIndex);
      }

      this.BScrollObj.refresh();
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      var _this$props$data$scro = this.props.data.scrollType,
          scrollType = _this$props$data$scro === void 0 ? new Date().getTime() : _this$props$data$scro;
      var initBScrollConfig = this.resetBScrollConfig();
      var wrapperNode = this.wrapper ? _reactDom.default.findDOMNode(this.wrapper) : null;

      if (wrapperNode) {
        this.BScrollObj = wrapperNode && new _betterScroll.default(wrapperNode, initBScrollConfig); // 某种情况下会出现无法滚动到指定的selectedIndex

        requestAnimationFrame(function () {
          _this2.BScrollObj.wheelTo(initBScrollConfig.wheel.selectedIndex);

          _this2.addActiveItem();

          _this2.BScrollObj.refresh();
        });
        this.BScrollObj.on('beforeScrollStart', this.touchStart);
        this.BScrollObj.on('scrollEnd', this.touchEnd);
        this.BScrollObj.on('scrollCancel', this.touchEnd);
        this.props.initBScrollCallback(scrollType, this.BScrollObj);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.BScrollObj.off('beforeScrollStart', this.touchStart);
      this.BScrollObj.off('scrollEnd', this.touchEnd);
      this.BScrollObj.off('scrollCancel', this.touchEnd);
    }
  }, {
    key: "resetBScrollConfig",
    value: function resetBScrollConfig() {
      var clsPrefix = this.context.clsPrefix;
      var data = this.props.data;
      var wheel = {
        selectedIndex: data.selectIndex ? data.selectIndex : 0,
        rotate: 0,
        adjustTime: 0,
        wheelWrapperClass: "".concat(clsPrefix, "-ws"),
        wheelItemClass: "".concat(clsPrefix, "-wi")
      };
      var initBScrollConfig = {};

      if (data.scrollData) {
        initBScrollConfig = data.scrollData;

        if (initBScrollConfig.wheel) {
          initBScrollConfig.wheel = Object.assign({}, initBScrollConfig.wheel, wheel);
        }
      } else {
        initBScrollConfig.wheel = wheel;
      }

      return initBScrollConfig;
    }
  }, {
    key: "getWrapper",
    value: function getWrapper(div) {
      this.wrapper = div;
    }
  }, {
    key: "getWheelScroll",
    value: function getWheelScroll(div) {
      this.wheelScroll = _reactDom.default.findDOMNode(div);
    }
  }, {
    key: "touchEnd",
    value: function touchEnd() {
      var _this$props$data = this.props.data,
          wrapperIndex = _this$props$data.wrapperIndex,
          scrollType = _this$props$data.scrollType;
      this.addActiveItem();
      this.props.onTouchEnd && this.props.onTouchEnd(scrollType != null ? scrollType : wrapperIndex, this.BScrollObj.getSelectedIndex());
    }
  }, {
    key: "touchStart",
    value: function touchStart() {
      this.removeActiveItem();
      this.props.onTouchStart && this.props.onTouchStart(this.props.wrapperIndex);
    }
  }, {
    key: "removeActiveItem",
    value: function removeActiveItem() {
      var clsPrefix = this.context.clsPrefix;

      if (this.wheelScroll.children[this.currIndex]) {
        this.wheelScroll.children[this.currIndex].className = this.wheelScroll.children[this.currIndex].className.replace(" ".concat(clsPrefix, "-web-picker__wi-active"), '');
      }
    }
  }, {
    key: "addActiveItem",
    value: function addActiveItem() {
      var clsPrefix = this.context.clsPrefix;
      this.currIndex = this.BScrollObj.getSelectedIndex();

      if (this.wheelScroll && this.wheelScroll.children[this.currIndex]) {
        this.wheelScroll.children[this.currIndex].className = "".concat(this.wheelScroll.children[this.currIndex].className, " ").concat(clsPrefix, "-web-picker__wi-active");
      }
    }
  }, {
    key: "renderItem",
    value: function renderItem() {
      var data = this.props.data;
      var clsPrefix = this.context.clsPrefix;
      var liCls = (0, _classnames.default)(data.itemClassName, "".concat(clsPrefix, "-web-picker__wi"));
      var timestamp = new Date().getTime();
      var liNodeList = data.listData.map(function (item, key) {
        return /*#__PURE__*/_react.default.createElement("li", {
          className: item.className ? (0, _classnames.default)(liCls, item.className) : liCls,
          "data-key": item.dataKey ? item.dataKey : item.text,
          key: "".concat(timestamp, "-").concat(key, "-item")
        }, item.text);
      });
      return liNodeList;
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var clsPrefix = this.context.clsPrefix;
      var _this$props = this.props,
          data = _this$props.data,
          wrapperKey = _this$props.wrapperKey,
          height = _this$props.height;
      var ulCls = (0, _classnames.default)(data.className, "".concat(clsPrefix, "-web-picker__wrapper--ws"));
      return /*#__PURE__*/_react.default.createElement("div", {
        key: wrapperKey,
        className: "".concat(clsPrefix, "-web-picker__wrapper wrapper"),
        ref: function ref(refs) {
          _this3.getWrapper(refs);
        }
      }, /*#__PURE__*/_react.default.createElement("ul", {
        className: ulCls,
        ref: function ref(refs) {
          _this3.getWheelScroll(refs);
        },
        style: {
          marginTop: "".concat(height / 2 - 25, "px")
        }
      }, this.renderItem()), /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(clsPrefix, "-web-picker__wrapper-bg")
      }));
    }
  }]);

  return PickerWrapper;
}(_react.default.PureComponent);

exports.default = PickerWrapper;

_defineProperty(PickerWrapper, "defaultProps", {
  wrapperKey: '',
  height: 187,
  data: []
});

_defineProperty(PickerWrapper, "contextType", _configContext.ConfigContext);